from selenium.webdriver import Chrome
from selenium.webdriver.common.by import By
import time
from yzm_code import base64_api
import json
"""
进行登录的思路
1. 点击我的
2. 获取用户名输入框   输入用户名
3. 获取密码输入框     输入密码
4. 识别验证码   获取验证码输入框  输入验证码
5. 点击登录
"""
driver = Chrome()
# 访问网址
driver.get('https://so.gushiwen.cn/shiwens/')
# 查找我的节点
mine = driver.find_element(By.XPATH, '//*[@id="html"]/body/div[1]/div[1]/div/div[2]/div[1]/a[6]')
# 点击我的
mine.click()
# 2. 获取用户名输入框   输入用户名
email = driver.find_element(By.ID, 'email')
email.send_keys('793390457@qq.com')
# 3. 获取密码输入框     输入密码
pwd = driver.find_element(By.ID, 'pwd')
pwd.send_keys('xlg17346570232')

# 4. 识别验证码   获取验证码输入框  输入验证码
# 获取验证码图片  截图
imgCode = driver.find_element(By.ID, 'imgCode')
img_path = 'yzm.jpg'
imgCode.screenshot(img_path)  # 验证码截图
# 进行识别
result = base64_api(uname='luckyboyxlg', pwd='17346570232', img=img_path, typeid=3)
print(result)
code = driver.find_element(By.ID, 'code')
code.send_keys(result)
time.sleep(3)
# 5. 点击登录
driver.find_element(By.ID, 'denglu').click()
time.sleep(3)
# 获取登录后的cookie
cookies = driver.get_cookies()
print(cookies)
with open('cookies.txt', 'w', encoding='utf-8') as f:
    f.write(json.dumps(cookies))
time.sleep(60)